<?php

class CommonController extends Controller {

	protected $db;
	protected $model;

	public function __construct($class) {

		parent::__construct($class);

		$this->db = DB::open('reading');

		$this->loginCheck();

		//$this->permissionCheck();
	}

	private function loginCheck() {
		if (!isset($_SESSION['reading_user_id']) || intval($_SESSION['reading_user_id']) <= 0) {
			redirect(url('auth/index'));
		}
	}

	private function permissionCheck(){
		global $controller, $action;

		$user_id = (int) $_SESSION['reading_user_id'];

		$sql = "SELECT permission FROM permission WHERE user_id=$user_id";

		$result = $this->db->query($sql);

		$permission = empty($result)?'':$result[0]['permission'];

		$currentPath = $controller.'/'.$action;

		$whitePermissions = array('home/index');

		if(!in_array($currentPath, explode(',',$permission)) && !in_array($currentPath, $whitePermissions)){
			setFlash('error','no permission');
			redirect(url('home/index'));
		}
	}
}
